package com.cls.business.strategy.impl.notice;

import com.cls.business.entity.*;
import com.cls.business.service.*;
import com.cls.business.strategy.NoticeStrategy;
import com.cls.common.enums.DictEnum;
import com.cls.common.enums.EmailTextEnum;
import com.cls.common.enums.MessageEnum;
import com.cls.common.exception.MyException;
import com.cls.common.utils.EmailSenderUtil;
import com.cls.system.entity.User;
import com.cls.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * 拟立项公告的实现方法
 *
 * @author Administrator
 */
@Component("notice_type_nlxgg")
@RequiredArgsConstructor
public class NLXGGNoticeImpl implements NoticeStrategy {

    private final INoticeService noticeService;
    private final ITopicPlanService topicPlanService;
    private final IDeclareTopicService declareTopicService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createNotice(Notice notice) {
        noticeService.save(notice);
        //拟立项公告发出之后，把所有的已评审状态的年度课题改成拟立项失败
        String year = notice.getYear();
        //根据年份查询年度课题
        TopicPlan topicPlan = topicPlanService.selectAnnualByYear(year);
        if (topicPlan == null) {
            throw new MyException("当前拟立项计划不存在！");
        }

        if (topicPlan.getPreItemNoticeId() != null) {
            throw new MyException("当前计划已被拟立项！");
        }
        //未提交拟立项申请的数据改成立项失败
        topicPlan.setPreItemNoticeId(notice.getNoticeId());
        topicPlan.setPreItemBeginTime(notice.getPreItemBeginTime());
        topicPlan.setPreItemEndTime(notice.getPreItemEndTime());
        topicPlanService.updateById(topicPlan);
        List<DeclareTopic> topicsByPlanIdAndStatusTypeCode = declareTopicService.findAnnualDeclareTopicByPlanIdAndStatusTypeCode(topicPlan.getPlanId(), DictEnum.CHECK_PASSED_REVIEW.getCode());
        topicsByPlanIdAndStatusTypeCode.forEach(item -> {
            item.setIsFinish(true);
            item.setStatusTypeCode(DictEnum.ITEM_FAILED.getCode());
            //更改状态
            declareTopicService.updateById(item);
        });

    }
}
